package zuo.from0516.basic2020.class02;

public class Code02_EvenTimesOddTimes {
    /**
     * arr中只有一种数出现奇数次
     * @param arr
     */
    public static void printOddTimesNum1(int[] arr){
        int eor = 0;
        for(int num : arr){
            eor ^= num;
        }
        System.out.println(eor);
    }

    /**
     * 有两种数出现了奇数次，找出这两种数
     * @param arr
     */
    public static void printOddTimesNum2(int[] arr){
        int eor = 0;//eor=a^b;
        for (int num : arr){
            eor ^= num;
        }
        int rightOne = eor & (-eor);
        int ans1 = 0;
        for (int i = 0; i < arr.length; i++) {
            if ((arr[i] & rightOne) != 0){
                ans1 ^= arr[i];
            }
        }
        int ans2 = ans1 ^ eor;
        System.out.println(ans1 + " " + ans2);

    }

    public static void main(String[] args) {
        int[] arr = {1,1,1,1,2,2,2,2,9,3,3,5,5,5,5,11};
        printOddTimeNum2Second(arr);
    }

    public static void printOddTimesNumSecond(int[] arrs){
        int eor = 0;
        for(int num : arrs){
            eor ^= num;
        }
        System.out.println(eor);
    }

    public static void printOddTimeNum2Second(int[] arrs){
        int eor = 0;
        for(int num : arrs){
            eor ^= num;
        }
        int rightOne = eor & (-eor);//拿到eor最右边的1
        int ans = 0;
        for (int num : arrs){
            if((num & rightOne) != 0){
                ans ^= num;
            }
        }
        int res = ans ^ eor;
        System.out.println(ans + " " + res);
    }
}
